package com.dd.ljzapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dd.ljzapplication.R;
import com.dd.ljzapplication.bean.EnumSpotCheckInfo;

import java.util.HashMap;
import java.util.List;

import static com.baidu.location.h.j.m;

/**
 * Created by LiJZ on 2016/11/10.
 */

public class LogAdapter extends BaseRVAdapter<EnumSpotCheckInfo.SpotChecksBean> {
	private HashMap<String, Integer> result = new HashMap<>();
	private boolean isClick = true;


	public LogAdapter(List<EnumSpotCheckInfo.SpotChecksBean> mListData, Context context) {
		super(mListData, context);
	}

	public LogAdapter(Context context, boolean isClick) {
		super(context);
		this.isClick = isClick;
	}

	public HashMap<String, Integer> getResult() {
		return result;
	}

	@Override
	public int getDisplayType(int postion) {
		return 0;
	}

	@Override
	public RecyclerView.ViewHolder onCreateDisplayHolder(ViewGroup parent, int viewType) {
		View view = mLayoutInflater.inflate(R.layout.item_log_spor_check, parent, false);
		MyHolder myHolder = new MyHolder(view);
//		myHolder.setIsRecyclable(false);
		return myHolder;
	}

	@Override
	public void onBindData(RecyclerView.ViewHolder holder, final int position) {
		final MyHolder myHolder = (MyHolder) holder;
		final EnumSpotCheckInfo.SpotChecksBean bean = mListData.get(position);
		myHolder.tvContent.setText(bean.getContent());

		if (bean.getType() == 1) {
			myHolder.radioGroup.setVisibility(View.VISIBLE);
			myHolder.radioGroup.clearCheck();

			if (bean.getResult() == -1) {
				myHolder.rbYes.setChecked(false);
				myHolder.rbNo.setChecked(false);
			} else if (bean.getResult() == 0) {
				myHolder.rbYes.setChecked(false);
				myHolder.rbNo.setChecked(true);
			} else if (bean.getResult() == 1) {
				myHolder.rbYes.setChecked(true);
				myHolder.rbNo.setChecked(false);
			}
		} else {
			myHolder.radioGroup.setVisibility(View.GONE);
		}

		myHolder.rbYes.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean checked = myHolder.rbYes.isChecked();
				if (checked) {
					result.put(bean.getContentID(), 1);
					bean.setResult(1);
				}
			}
		});

		myHolder.rbNo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean checked = myHolder.rbNo.isChecked();
				if (checked) {
					result.put(bean.getContentID(), 0);
					bean.setResult(0);
				}
			}
		});

		if (!isClick) {
			myHolder.rbNo.setClickable(false);
			myHolder.rbYes.setClickable(false);
		}

	}

	class MyHolder extends RecyclerView.ViewHolder {
		TextView tvContent;
		RadioGroup radioGroup;
		RadioButton rbYes;
		RadioButton rbNo;

		public MyHolder(View itemView) {
			super(itemView);
			tvContent = (TextView) itemView.findViewById(R.id.tv_content);
			radioGroup = (RadioGroup) itemView.findViewById(R.id.rg);
			rbYes = (RadioButton) itemView.findViewById(R.id.rb_yes);
			rbNo = (RadioButton) itemView.findViewById(R.id.rb_no);
		}
	}

}
